<?php

namespace Modules\Shop\Http\Controllers;

use App\Http\Requests\RequestShopUser;
use HiCommon\Service\PermissionsService;
use HiCommon\Service\RoleService;
use HiCommon\Service\ShopUserService;
use Illuminate\Support\Facades\Request;

class ShopUserController extends BaseController
{

    /**
     * 安全设置页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function safety_page(){
        try {
            $n_uid = intval(shop_user_id());
            $res = ShopUserService::api_find_one_user_shop($n_uid);
            echo_error($res);
            $arr_data = [
              'arr_data'=>$res['data']
            ];
//            dd($arr_data);
            return view('shop::shop_user.safety_page',$arr_data);
        }
        catch (\Exception $exception) {
            msg_error('fail-'.$exception->getMessage());
        }
    }

    /**
     * 修改登录密码
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function safety_update_password(){
        try {
            $arr_input = Request::input();
            $res = ShopUserService::api_edit_password($arr_input);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('fail-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /*设置支付密码*/
    public function safety_pay_password_set(){
        try {
            $arr_input = Request::input();
            $res = ShopUserService::api_set_pay_password($arr_input);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('fail-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*修改登录手机号*/
    public function safety_update_phone(){
        try {
            $arr_input = Request::input();
            $res = ShopUserService::api_edit_phone($arr_input);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('fail-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*用户资料页面*/
    public function userinfo_page(){
        try {
            $n_uid = intval(shop_user_id());
            $res = ShopUserService::api_find_one($n_uid);
            echo_error($res);
            $arr_data = [
              'arr_data'=>$res['data']
            ];
//            dd($arr_data);
            return view('shop::shop_user.userinfo_page',$arr_data);
        }
        catch (\Exception $exception) {
            msg_error('fail-'.$exception->getMessage());
            return 'ok';
        }
    }

    /*修改用户资料*/
    public function userinfo_update(){
        try {
            $arr_input = Request::input();
            $res = ShopUserService::api_edit_userinfo($arr_input);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('fail-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*角色列表*/
    public function role_list(){
        try {
            $n_shop_id = shop_shop_id();
            $res = RoleService::get_all_roles_for_shop($n_shop_id);
            echo_error($res);
            return view('shop::shop_user.role_list', $res);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*添加角色页面*/
    public function role_create(){
        try {
            $n_uid = intval(shop_user_id());
            $arr_data = PermissionsService::api_shop_for_whole_permissions($n_uid);
            $arr_data = [
              'arr_data'=>json_encode($arr_data)
            ];
            return view('shop::shop_user.role_create', $arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }
    
    /*添加角色操作*/
    public function role_store(){
        try {
            $arr_input = Request::input();
            $arr_input['model'] = 'shop';
            $arr_input['shop_id'] = shop_shop_id();
            return RoleService::add_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*修改角色页面*/
    public function role_edit(){
        try {
            $n_id = intval(Request::get('id',0));
            $res = RoleService::get_one($n_id);
            echo_error($res);
            $arr_data['arr_data'] = $res['data'];

            //查询所有权限并标记当前角色选择的权限
            $n_uid = intval(shop_user_id());
            $arr_permission = PermissionsService::api_shop_for_whole_permissions_with_selected($n_uid,$n_id);
            $arr_data['arr_permission'] = json_encode($arr_permission);

            return view('shop::shop_user.role_edit',$arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*修改角色操作*/
    public function role_update(){
        try {
            $arr_input = Request::input();
            return RoleService::edit_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*删除角色*/
    public function role_del(){
        try {
            $n_id = intval(Request::get('id',0));
            return RoleService::delete_one($n_id);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*账号列表*/
    public function user_list(){
        try {
            $arr_search = Request::input();
            $arr_user = shop_user()->toarray();
            $arr_search['shop_id'] = shop_shop_id();
            $res = ShopUserService::api_search_list($arr_search);
            echo_error($res);

            $res['data']['arr_user'] = $arr_user;
            return view('shop::shop_user.user_list', $res['data']);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /*添加账号-页面*/
    public function user_create(){
        try {
            $n_shop_id = shop_shop_id();
            $res = RoleService::get_all_roles_for_shop($n_shop_id);
            echo_error($res);

            $arr_data = [
              'arr_role'=>$res['data'],
              'arr_sex'=>[0=>'保密',1=>'男',2=>'女'],
            ];
            return view('shop::shop_user.user_create', $arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*添加账号-操作*/
    public function user_store(RequestShopUser $request){
        try {
            $arr_input = $request->input();
            $arr_input['shop_id'] = shop_shop_id();
            shiwu_start();
            $res = ShopUserService::api_add_one($arr_input);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*修改账号-页面*/
    public function user_edit(){
        try {
            $n_id = intval(Request::get('id',0));
            $arr_user = shop_user()->toarray();
            $n_shop_id = intval($arr_user['shop_id']);
            $res = RoleService::get_all_roles_for_shop($n_shop_id);
            echo_error($res);
            $arr_data = [
              'arr_role'=>$res['data'],
              'arr_sex'=>[0=>'保密',1=>'男',2=>'女'],
            ];

            $res = ShopUserService::api_find_one($n_id);
            echo_error($res);
            $arr_data['arr_data'] = $res['data'];
            $arr_data['arr_user'] = $arr_user;
            
            return view('shop::shop_user.user_edit',$arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*修改账号-操作*/
    public function user_update(RequestShopUser $request){
        try {
            $arr_input = $request->input();
            return ShopUserService::api_edit_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*账号启用禁用*/
    public function user_change_state(){
        try {
            $arr_input = Request::input();
            shiwu_start();
            $res = ShopUserService::api_change_one_state($arr_input);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /*删除账号*/
    public function user_del(){
        try {
            $n_id = (int)Request::get('id',0);
            shiwu_start();
            $res = ShopUserService::api_delete_one($n_id);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }


}
